
use strict;
use warnings;

use Test::More tests => 6;

use t::TestUtils;

my $handler = test_dir() . "/basic_handler.js";

test_apache(
  "",
  sub {
    my $response = user_agent()->get(test_url_base() . '/');
    cmp_ok($response->content, 'eq', default_content(), 'not handled without config');
  }
);

test_apache(
  "JavascriptHandler $handler",
  sub { # tests
    my $response = user_agent()->get(test_url_base() . '/');
    cmp_ok($response->content, 'eq', 'Hello World! (from js)', 'generate javascript content');
  }
);

test_apache(
  qq{
    <Location "/blah">
      JavascriptHandler $handler
    </Location>
  },
  sub { # tests
    {
      my $response = user_agent()->get(test_url_base() . '/');
      cmp_ok($response->content, 'eq', default_content(), 'not handled outside of location');
    }
    {
      my $response = user_agent()->get(test_url_base() . '/blah/blah');
      cmp_ok($response->content, 'eq', 'Hello World! (from js)', 'handled inside location');
    }
  }
);

my $port = test_port();

test_apache(
  qq{
    NameVirtualHost 127.0.0.1:$port

    <VirtualHost 127.0.0.1:$port>
      ServerName nojs.server.name
    </VirtualHost>

    <VirtualHost 127.0.0.1:$port>
      ServerName js.server.name
      JavascriptHandler $handler
    </VirtualHost>
  },
  sub {
    {
      my $response = user_agent()->get(test_url_base() . '/', Host => 'nojs.server.name');
      cmp_ok($response->content, 'eq', default_content(), 'not handled in other virtual host');
    }
    {
      my $response = user_agent()->get(test_url_base() . '/blah', Host => 'js.server.name');
      cmp_ok($response->content, 'eq', 'Hello World! (from js)', 'handled inside virtual host');
    }
  }
);

